<?php

namespace mpend\modules\notice\controllers;

use common\models\notice\Notice;
use mpend\modules\notice\models\MessageSearch;
use mpend\modules\notice\models\NoticeView;
use mpend\modules\notice\models\UnreadMsgForm;
use Yii;
use common\models\notice\NoticeReceive;
use mpend\components\Controller;
use yii\db\Exception;

class MessageController extends Controller
{
    /**
     * 向简历发送通告(邀请)
     */
    public function actionPushNotice()
    {
        $model = new NoticeReceive();
        $model->from_user_id = $this->uid;
        $model->title = '向您投递了通告';
        $model->type = 'notice';
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('缺少信息');
        }
        return $model->save();
    }

    /**
     * 应邀
     */
    public function actionNoticeAnswer()
    {
        $model = new NoticeReceive();
        $model->from_user_id = $this->uid;
        $model->title = '接受了您的邀请';
        $model->type = 'answer';
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('缺少信息');
        }
        return $model->save();
    }

    /**
     * 投简历
     */
    public function actionSendResume()
    {
        $model = new NoticeReceive();
        $model->from_user_id = $this->uid;
        $model->title = '向您投递了简历';
        $model->type = 'resume';
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('缺少信息');
        }
        return $model->save();
    }

    /**
     * 获取消息列表
     */
    public function actionNoticeMsgList()
    {
//        /* @var $model NoticeReceive */
//        $model = NoticeReceive::find()->active()->andWhere(['user_id' => $this->uid])->all();
//        return $model;
//        $params = Yii::$app->request->post();
//        $user_id = $this->uid;
//        $type = $params['type'];
//        if ($params) {
//            /* @var $model NoticeReceive */
//            $model = NoticeReceive::find()->active()->andWhere(['id' => $params['id']])->one();
//        }
        $params = Yii::$app->request->post();
        $model = new MessageSearch();
        $model->load($params, '');
        $model->user_id = $this->uid;
        return $model->search();
    }

    /**
     * 获取未读消息
     */
    public function actionUnreadMsg()
    {
        $query = NoticeReceive::find()->active()->andWhere([
            'user_id' => $this->uid,
            'status' => 0
        ]);
        $returndata['total'] = $query->count();
        $query1 = clone $query;
        $query2 = clone $query;
        $query3 = clone $query;
        $returndata['system'] = $query->andWhere(['type'=>'system'])->count();
        $returndata['notice'] = $query1->andWhere(['type'=>'notice'])->count();
        $returndata['answer'] = $query2->andWhere(['type'=>'answer'])->count();
        $returndata['resume'] = $query3->andWhere(['type'=>'resume'])->count();
        return $returndata;
    }

    /**
     * 读取消息
     */
    public function actionReadNoticeMsg()
    {
        $params = Yii::$app->request->post();
        if ($params) {
            /* @var $model NoticeReceive */
            $model = NoticeReceive::find()->active()->andWhere(['id' => $params['id']])->one();
            if (!$model) {
                return parent::error('消息不存在');
            }
            // 将消息状态改为已读
            $model->setAttributes([
                'status' => '1'
            ]);
            $transaction = Yii::$app->db->beginTransaction();
            try {
                if (!$model->save()) {
                    throw new Exception($model->getErrorMsg());
                }
                $transaction->commit();
            } catch (Exception $e) {
                // 记录回滚（事务回滚）
                $transaction->rollBack();
            }
            return [];
        } else {
            return parent::error('缺少信息');
        }
    }
}